<?php 
include('common_include.php');
include('validateSession.php');

$factoryArr = array();
$dao = new SQLitePersistence();
$factoryArr = $dao->getAllFactory();
unset($dao);
?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<script language="javascript" src="scripts/common.js"></script>
<title>
	Update Factory
</title>
<script>

function submitForAdd() {
	var opsElem = document.forms[0].operation;
	if (opsElem) {
		opsElem.value = 'Add';
	}
	var countryText = trim(document.forms[0].country.value);
	var addressText = trim(document.forms[0].address.value);
	if (countryText == '') {
		alert('Please enter the country for new factory location.');
		document.forms[0].country.focus();
		return;
	}
	if (addressText == '') {
		alert('Please enter the address for new factory address.');
		document.forms[0].address.focus();
		return;
	}	
	document.forms[0].submit();
}

function submitForUpdate() {
	var factoryResultSize = <?php echo sizeof($factoryArr);?>;
	if (!validateSequences(factoryResultSize)) {
		return;
	}
	for (i=0; i < factoryResultSize; i++) {
		var countryElem = document.getElementById('factory[' + i + '][country]');
		if (countryElem) {
			if (trim(countryElem.value) == '') {
				alert('Country of the factory cannot be empty.');
				return;
			}
		}
	}
	for (i=0; i < factoryResultSize; i++) {
		var addressElem = document.getElementById('factory[' + i + '][address]');
		if (addressElem) {
			if (trim(addressElem.value) == '') {
				alert('Address of the factory cannot be empty.');
				return;
			}
		}
	}

	var opsElem = document.forms[0].operation;
	if (opsElem) {
		opsElem.value = 'Update';
	}
	document.forms[0].submit();
}

function submitForDelete(factoryID) {
	var opsElem = document.forms[0].operation;
	if (opsElem) {
		opsElem.value = 'Delete';
	}	
	document.forms[0].deleteID.value = factoryID;
	document.forms[0].submit();
}

function initAllSelectComponents() {
	var factoryResultSize = <?php echo sizeof($factoryArr);?>; 

	for (i=0; i < factoryResultSize; i++) {
		var selectComponent = document.getElementById('sequence_' + i);
		var initSeqComponent = document.getElementById('initSeq_' + i);
		if (selectComponent) {
			for (j=0; j < factoryResultSize; j++) {
				var optionElem = document.createElement('option');
				optionElem.text = String(j+1);
				optionElem.value = String(j+1);
				if (optionElem.value == initSeqComponent.value) {
					optionElem.selected = true;
				}
			    try {
			    	selectComponent.add(optionElem, null); // standards compliant; doesn't work in IE
				}
				catch(ex) {
					selectComponent.add(optionElem); // IE only
				}
			}
		}
	}
}

</script>
</head>
<body onload="initAllSelectComponents();">

<form action="UpdateFactoryController.php" method="post">
<table>
	<tr>
		<td class="errorLabel">
			<?php echo $_GET['error']; ?>
		</td>
	</tr>
	<tr>
		<td class="labelClass">
			Factory Listing:
		</td>
	</tr>
	<tr>
		<td class="labelClass">
			<table class="listingTable"> 
				<tr class="yellow">
					<td class="adjacent">
						Country of the Factory Location:
					</td>
					<td class="adjacent">
						Address of the Factory Location:
					</td>
					<td class="adjacent">
						Order Sequence:
					</td>					
					<td class="adjacent">
						&nbsp;
					</td>
				</tr>
				<?php for ($i=0; $i < sizeof($factoryArr); $i++) {
					$rowIDAttr = 'sequence_' . $i;
					$hiddenInitSeq = 'initSeq_' . $i;
					$aFactory = $factoryArr[$i];
					$submitForDeleteOnClickEvent = 'submitForDelete(' . $aFactory->getFactoryId() . ');';
					$factoryIDHidden = 'factory[' . $i . '][factoryID]';
					$factoryCountry = 'factory[' . $i . '][country]';
					$factoryAddress = 'factory[' . $i . '][address]';
					$factorySeq = 'factory[' . $i . '][factorySeq]';
				?>
				<tr>
					<td class="adjacent">
						<input type="hidden" name="<?php echo $factoryIDHidden?>" value="<?php echo $aFactory->getFactoryId();?>"/>
						<input type="text" id="<?php echo $factoryCountry?>" name="<?php echo $factoryCountry?>" value="<?php echo $aFactory->getFactoryCountry();?>" /> 
					</td>
					<td class="adjacent">
						<input type="text" id="<?php echo $factoryAddress?>" name="<?php echo $factoryAddress?>" value="<?php echo $aFactory->getFactoryAddress();?>" /> 
					</td>					
					<td class="adjacent">
						<select name="<?php echo $factorySeq?>" id="<?php echo $rowIDAttr?>">
						</select>
					</td>
					<td class="adjacent">
						<input type="button" value="Delete" onclick="<?php echo $submitForDeleteOnClickEvent?>"/>
						<input type="hidden" id="<?php echo $hiddenInitSeq?>" name="<?php echo $hiddenInitSeq?>" value="<?php echo $aFactory->getFactorySeq();?>"/>
					</td>					
				</tr>
				<?php } 
				unset($factoryArr);
				?>
			</table>
		</td>
	</tr> 	
	<tr>
		<td>
			<input type="button" value="Update" onclick="submitForUpdate();"/>
		</td>
	</tr>		
	<tr>
		<td class="labelClass">
			&nbsp;
		</td>
	</tr>	
	<tr>
		<td class="labelClass">
			&nbsp;
		</td>
	</tr>	
	<tr>
		<td class="labelClass">
			 Adding New Factory:
		</td>
	</tr>	
	<tr>
		<td>
			<table width="100%">
				<tr>
					<td class="textLabelClass">
						Country:					
					</td>
					<td class="textLabelClass">
						Address:
					</td>					
				</tr>			
				<tr>
					<td>
						<input type="text" name="country" />					
					</td>
					<td>
						<input type="text" name="address" />
					</td>					
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td>
			<input type="button" value="Add" onclick="submitForAdd();"/>
		</td>
	</tr>	
	<tr>
		<td>
			<input type="hidden" name="deleteID" />
			<input type="hidden" name="operation" />
		</td>
	</tr>
</table>
</form>

</body>
</html>